package com.dorso.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.dorso.entity.Product;
import org.apache.ibatis.annotations.*;


@Mapper
public interface ProductDao extends BaseMapper<Product> {
    @Insert("insert into product_supplier (product_id,supplier_id) values (#{productId},#{supplierId})")
    public void insertProductSupplier(long productId,long supplierId);
    @Delete("delete from product_supplier where product_id = #{productId}")
    public void deleteSupplierByProduct(long productId);
    @Select("select distinct p.* from product p" +
            " left join product_supplier ps on p.id=ps.product_id " +
            " left join supplier s on s.id=ps.supplier_id " +
            " [${ew.customSqlSegment}]") //由QueryWrapper转换
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "supplierList", column = "id",
                    many = @Many(select = "com.dorso.dao.SupplierDao.findSuppliersByProduct"))})
    public IPage<Product> findProductsBySupplier(IPage<Product> page, @Param(Constants.WRAPPER) QueryWrapper<Product> wrapper);
    @Select("select distinct p.* from product p ${ew.customSqlSegment}")
    @Results({@Result(id = true, property = "id", column = "id"),
            @Result(property = "supplierList", column = "id",
                    many = @Many(select = "com.dorso.dao.SupplierDao.findSuppliersByProduct"))})
    public IPage<Product>findProducts(IPage<Product>page ,@Param(Constants.WRAPPER)QueryWrapper<Product>wrapper);
}
